package cn.com.dcsgo.enums;

import lombok.Getter;

/**
 * 订单状态枚举类
 *
 * @author Dcsgo
 * @since 2025/11/16 14:07
 */
@Getter
public enum OrderStatus {
    /**
     * 待付款
     */
    PENDING_PAYMENT(1, "待付款"),

    /**
     * 待发货
     */
    PENDING_SHIPMENT(2, "待发货"),

    /**
     * 待收货
     */
    PENDING_RECEIPT(3, "待收货"),

    /**
     * 待评价
     */
    PENDING_EVALUATION(4, "待评价"),

    /**
     * 交易成功
     */
    SUCCESS(5, "成功"),

    /**
     * 交易失败
     */
    FAILED(6, "失败");

    /**
     * 获取状态码
     */
    private final int code;
    /**
     * 获取状态描述
     */
    private final String description;

    /**
     * 构造函数
     *
     * @param code        状态码
     * @param description 状态描述
     */
    OrderStatus(int code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 根据状态码获取枚举实例
     *
     * @param code 状态码
     * @return 对应的枚举实例
     * @throws IllegalArgumentException 如果状态码不存在
     */
    public static OrderStatus valueOf(int code) {
        for (OrderStatus status : values()) {
            if (status.code == code) {
                return status;
            }
        }
        throw new IllegalArgumentException("无效的订单状态码: " + code);
    }

    /**
     * 检查状态码是否存在
     *
     * @param code 状态码
     * @return 是否存在
     */
    public static boolean contains(int code) {
        for (OrderStatus status : values()) {
            if (status.code == code) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取所有状态码
     *
     * @return 状态码数组
     */
    public static int[] getAllCodes() {
        OrderStatus[] statuses = values();
        int[] codes = new int[statuses.length];
        for (int i = 0; i < statuses.length; i++) {
            codes[i] = statuses[i].code;
        }
        return codes;
    }

    @Override
    public String toString() {
        return "OrderStatus{" +
                "code=" + code +
                ", description='" + description + '\'' +
                '}';
    }
}
